pandas ewm var and std | 您所在的位置:网站首页 › pandas std › pandas ewm var and std |
百度翻译此文
有道翻译此文
问题描述
I unsuccessfully tried to replicate the calculation of exponential weighted moving variance. here is the code I used. import pandas as pd import numpy as np l = [12., 12.5, 13.1, 14.6, 17.8, 19.1, 24.5] df = pd.DataFrame(data=l, columns=['data']) N = 5 a = 2./(1+N) bias = (2-a)/2./(1-a) ewma = df.ewm(span=N).mean() var_pandas = df.ewm(span=N, adjust=False).var() var_calculated = (1-a) * (var_pandas.shift(1) + bias * a * (df - ewma.shift(1))**2) var_pandas Out[100]: data 0 NaN 1 0.125000 2 0.359231 3 1.582143 4 7.157121 5 10.080647 6 26.022245 var_calculated Out[101]: data 0 NaN 1 NaN 2 0.261111 3 1.264610 4 6.246149 5 9.135133 6 24.123265as you would see I still have a difference that I couldn't figure out. Grateful for your insights! I used the above formula from: pandas ewm.std calculation 推荐答案Copy-pasted the code posted by kosnik and build it up to answer this question. below: # Import libraries import numpy as np import pandas as pd # Create DataFrame l = [12., 12.5, 13.1, 14.6, 17.8, 19.1, 24.5] df = pd.DataFrame(data=l, columns=['data']) # Initialize N = 5 # Span a = 2./(1+N) # Alpha # Use .evm() to calculate 'exponential moving variance' directly var_pandas = df.ewm(span=N).var() # Initialize variable varcalc=[] # Calculate exponential moving variance for i in range(0,len(df.data)): # Get window z = np.array(df.data.iloc[0:i+1].tolist()) # Get weights: w n = len(z) w = (1-a)**np.arange(n-1, -1, -1) # This is reverse order to match Series order # Calculate exponential moving average ewma = np.sum(w * z) / np.sum(w) # Calculate bias bias = np.sum(w)**2 / (np.sum(w)**2 - np.sum(w**2)) # Calculate exponential moving variance with bias ewmvar = bias * np.sum(w * (z - ewma)**2) / np.sum(w) # Calculate standard deviation ewmstd = np.sqrt(ewmvar) varcalc.append(ewmvar) #print('ewmvar:',ewmvar) #varcalc df['var_pandas'] = var_pandas df['varcalc'] = varcalc df |
CopyRight 2018-2019 实验室设备网 版权所有 |